package org.platform.service.diagnose;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.mc.mic.core.log.bus.BusLog;
import com.mc.mic.core.log.bus.BusLogMapper;
import org.platform.dao.mapper.FdStepInfoMapper;
import org.platform.dao.mapper.FdTaskProcessDataMapper;
import org.platform.mo.bussmo.diagnose.test.TestResultContent;
import org.platform.mo.csmo.cmd41005.QueryDiagnoseRecordRequestParam;
import org.platform.mo.csmo.cmd41005.QueryDiagnoseRecordResponseParam;
import org.platform.mo.csmo.cmd41006.QueryDiagnoseStepRequestParam;
import org.platform.mo.csmo.cmd41006.QueryDiagnoseStepResponseParam;
import org.platform.mo.csmo.cmd41007.QueryTestDiagnoseResultRequestParam;
import org.platform.mo.csmo.cmd41007.QueryTestDiagnoseResultResponseParam;
import org.platform.mo.csmo.cmd41008.QueryTreeDiagnoseDetailRequestParam;
import org.platform.mo.csmo.cmd41008.QueryTreeDiagnoseDetailResponseParam;
import org.platform.mo.csmo.cmd41009.QueryLinkTestResultRequestParam;
import org.platform.mo.csmo.cmd41009.QueryLinkTestResultResponseParam;
import org.platform.mo.csmo.cmd41010.QueryBerTestResultRequestParam;
import org.platform.mo.csmo.cmd41010.QueryBerTestResultResponseParam;
import org.platform.mo.csmo.cmd41011.QueryGtTestResultRequestParam;
import org.platform.mo.csmo.cmd41011.QueryGtTestResultResponseParam;
import org.platform.mo.csmo.common.FD_BusLog;
import org.platform.mo.csmo.common.FD_TestDiagnoseResult;
import org.platform.mo.csmo.common.StepInfo;
import org.platform.mo.csmo.common.TaskProcessData;
import org.platform.mo.db.FdStepInfo;
import org.platform.mo.db.FdTaskProcessData;
import org.platform.processor.diagnose.DiagnoseResultProcessor;
import org.platform.processor.diagnose.DiagnoseTaskProcessor;
import org.platform.processor.knowledge.TreeProcessor;
import org.platform.service.IBaseService;
import org.platform.tool.util.BeansUtils;
import org.platform.tool.util.JsonUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author FengJie
 * @date 2018/7/19
 */
@Service
public class DiagnoseRecordServiceImpl implements IBaseService {
    @Autowired
    private FdTaskProcessDataMapper processDataMapper;
    @Autowired
    private FdStepInfoMapper stepInfoMapper;
    @Autowired
    private DiagnoseResultProcessor diagnoseResultProcessor;
    @Autowired
    private TreeProcessor processor;
    @Autowired
    private DiagnoseTaskProcessor taskProcessor;
    @Autowired
    private BusLogMapper logMapper;

    /**
     * 查询诊断记录（41005）
     *
     * @param request
     * @return
     */
    public QueryDiagnoseRecordResponseParam queryDiagnoseRecord(QueryDiagnoseRecordRequestParam request) {
        QueryDiagnoseRecordResponseParam response = new QueryDiagnoseRecordResponseParam();
        Example example = new Example(BusLog.class);
        example.createCriteria().andLike("taskId", "%" + request.getDiagnoseId());
        example.orderBy("timeTag").desc();
        List<BusLog> busLogList = logMapper.selectByExample(example);
        List<FD_BusLog> logList = BeansUtils.copyListProperties(busLogList, FD_BusLog.class);
        response.setLogList(logList);
        return response;
    }

    /**
     * * 查询诊断测试步骤(41006)
     *
     * @param requestParam
     * @return
     */
    public QueryDiagnoseStepResponseParam queryDiagnoseStep(QueryDiagnoseStepRequestParam requestParam) {
        QueryDiagnoseStepResponseParam responseParam = new QueryDiagnoseStepResponseParam();
        String diagnoseId = requestParam.getDiagnoseId();
        FdTaskProcessData processData = processDataMapper.selectByPrimaryKey(diagnoseId);

        TaskProcessData taskProcessData = new TaskProcessData();
        BeanUtils.copyProperties(processData, taskProcessData);

        Example example = new Example(StepInfo.class);
        example.createCriteria().andEqualTo("taskProcessId", diagnoseId);
        List<FdStepInfo> stepList = stepInfoMapper.selectByExample(example);
        List<StepInfo> stepInfo = BeansUtils.copyListProperties(stepList, StepInfo.class);
        taskProcessData.setStepInfoList(stepInfo);

        responseParam.setTaskProcessData(taskProcessData);
        return responseParam;
    }

    /**
     * 测试结果查询(41007)
     */
    public QueryTestDiagnoseResultResponseParam queryTestDiagnoseResult(QueryTestDiagnoseResultRequestParam requestParam) throws JsonProcessingException {
        QueryTestDiagnoseResultResponseParam responseParam = new QueryTestDiagnoseResultResponseParam();
        String diagnoseId = requestParam.getDiagnoseId();
        TestResultContent result = diagnoseResultProcessor.queryTestDiagnoseResult(diagnoseId);
        String jsonResult = JsonUtils.beanToJson(result, true);
        responseParam.setResult(new FD_TestDiagnoseResult(jsonResult));
        return responseParam;
    }

    /**
     * 查询故障树诊断结果(41008)
     *
     * @param requestParam
     * @return
     */
    public QueryTreeDiagnoseDetailResponseParam queryTreeDiagnoseDetail(QueryTreeDiagnoseDetailRequestParam requestParam) {
        QueryTreeDiagnoseDetailResponseParam responseParam = new QueryTreeDiagnoseDetailResponseParam();
        responseParam.setTreeNodeList(processor.queryTreeNodeByDiagnoseId(requestParam.getDiagnoseId()));
        return responseParam;
    }

    /**
     * 查询链路测试结果(41009)
     */
    public QueryLinkTestResultResponseParam queryLinkTestResult(QueryLinkTestResultRequestParam requestParam) {
        QueryLinkTestResultResponseParam responseParam = new QueryLinkTestResultResponseParam();
        responseParam.setSignalLevel(diagnoseResultProcessor.getLinkTestResult(taskProcessor.getDeviceLink(requestParam.getDeviceLinkList()), requestParam.getLoopType()));
        return responseParam;
    }

    /**
     * 查询误码率测试结果(41010)
     */
    public QueryBerTestResultResponseParam queryBerTestResult(QueryBerTestResultRequestParam requestParam) {
        QueryBerTestResultResponseParam responseParam = new QueryBerTestResultResponseParam();

        responseParam.setMeasureebn0List(diagnoseResultProcessor.getBerTestResult(
                taskProcessor.getDeviceLink(requestParam.getDeviceLinkList()),
                requestParam.getViterbidecoderI(),
                requestParam.getViterbidecoderQ(),
                requestParam.getRs223(),
                requestParam.getRs239(),
                requestParam.getLdpc(),
                requestParam.getDemodulatorid()
        ));
        return responseParam;
    }

    /**
     * 查询GT值测试结果(41011)
     */
    public QueryGtTestResultResponseParam queryGtTestResult(QueryGtTestResultRequestParam requestParam) {
        QueryGtTestResultResponseParam responseParam = new QueryGtTestResultResponseParam();
        responseParam.setGtList(diagnoseResultProcessor.getGtTestResult(
                requestParam.getAntennaId(),
                requestParam.getRadioSource(),
                requestParam.getFrequency()));
        return responseParam;
    }

}
